<?php

namespace App\Services\YunXinCloud;


use App\Constants\CodeData;
use App\Exception\MessageException;
use App\Utils\LoggerUtil;
use Throwable;
use function Hyperf\Config\config;

class WyyChatRoomClient extends WyyClient
{

    /**
     * Note - 创建聊天室
     * DateTime 2024/8/29 19:10
     * User Admin
     * @param $accid -聊天室属主的 IM 账号（accid）
     * @param $name -聊天室名称
     * @param $announcement -聊天室公告
     * @param array $ext -扩展字段
     * @param int $queuelevel -队列管理权限。0：所有人都有权限变更队列，1：只有主播管理员才能操作变更。
     * @param int $inOutNotification -是否关闭人员进出聊天室事件通知。0：关闭；1：开启
     * @return mixed
     */
    public function createChatRoom($accid, $name, $announcement, array $ext = [], int $queuelevel = 1, int $inOutNotification = 0)
    {
        $url = '/chatroom/create.action';
        $data = array(
            'creator' => $accid,
            'name' => $name,
            'announcement' => $announcement,
            'ext' => json_encode($ext),
            'queuelevel' => $queuelevel,
            'inOutNotification' => $inOutNotification
        );
        $result = $this->postDataCurl($url, $data);
        if ($result['code'] !== 200) {
            LoggerUtil::write('create_chat_room_fail', json_encode($result));
            throw new MessageException('创建聊天室失败');
        }
        return $result['chatroom'];
    }

    /**
     * Note - 更新聊天室
     * DateTime 2024/8/29 18:54
     * User Admin
     * @param $room_id [房间id]
     * @param $name [房间名称]
     * @param $announcement [房间公告]
     * @param array $ext [扩展信息]
     * @param bool $needNotify
     * @return mixed
     */
    public function updateChatRoom($room_id, $name, $announcement, array $ext = [], bool $needNotify = false, $queuelevel = 1)
    {
        $url = '/chatroom/update.action';
        $data = array(
            'roomid' => $room_id,
            'name' => $name,
            'announcement' => $announcement,
            'needNotify' => $needNotify,
            'notifyExt' => json_encode($ext),
            'queuelevel' => $queuelevel
        );
        $result = $this->postDataCurl($url, $data);
        if ($result['code'] !== 200) {
            LoggerUtil::write('update_chat_room_fail', json_encode($result));
            throw new MessageException('更新聊天室失败');
        }
        LoggerUtil::write('update_chat_room', json_encode($result));
        return $result['chatroom'];
    }


    /**
     * Note - 开放/关闭聊天室
     * DateTime 2024/8/30 15:08
     * User Admin
     * @param $roomId
     * @param $operator
     * @param $valid
     * @return mixed
     */
    public function toggleCloseStat($roomId, $operator, $valid)
    {
        $url = '/chatroom/toggleCloseStat.action';
        $data = array(
            'roomid' => $roomId,
            'operator' => $operator,
            'valid' => $valid, // false：关闭聊天室，true：开放聊天室
        );
        $result = $this->postDataCurl($url, $data);
        if ($result['code'] !== 200) {
            LoggerUtil::write('close_chat_room_fail', json_encode($result));
            throw new MessageException('更新聊天室失败');
        }
        LoggerUtil::write('close_chat_room', $result);
        return $result['desc'];
    }


    /**
     * Note - 设置聊天室成员角色
     * DateTime 2024/8/30 16:25
     * User Admin
     * @param $roomId -聊天室id
     * @param $operator -操作者账号accid
     * @param $target -被操作者账号accid
     * @param $opt
     * 1: 设置为管理员，operator必须是创建者
     * 2:设置普通等级用户，operator必须是创建者或管理员
     * -1:设为黑名单用户，operator必须是创建者或管理员
     * -2:设为禁言用户，operator必须是创建者或管理员
     * @param bool $optvalue
     * @return array
     */
    public function setMemberRole($roomId, $operator, $target, $opt, bool $optvalue = true): array
    {
        $url = "/chatroom/setMemberRole.action";
        $args = [
            "roomid" => $roomId,
            "operator" => strval($operator),
            "target" => strval($target),
            "opt" => $opt,
            "optvalue" => $optvalue,//true:设置；false:取消设置
        ];
        $result = $this->postDataCurl($url, $args);
        if ($result['code'] !== 200) {
            LoggerUtil::write('set_member_role', json_encode($result));
            throw new MessageException($result['desc'], CodeData::NEED_TIPS, [], false);
        }
        return $result;

    }

    /**
     * Note - 踢人出聊天室
     * DateTime 2024/9/3 12:27
     * User Admin
     * @param mixed $roomId
     * @param $operator
     * @param $target
     * @return bool
     */
    public function kickMember(mixed $roomId, $operator, $target)
    {

        $url = "/chatroom/kickMember.action";
        $args = [
            "roomid" => $roomId,
            "targetAccid" => $target,
            "accid" => $operator,
        ];
        logInfo("kick_member", ['uid' => $this->from, 'args' => $args]);
        $result = $this->postDataCurl($url, $args);
        logInfo("kick_member", ['uid' => $this->from, 'result' => $result]);
        if ($result["code"] !== 200) {
            // throw new \Exception($result['desc']);
            throw new MessageException($result['desc'], CodeData::NEED_TIPS, [], false);
        }

        return true;

    }


    /**
     * Note - 禁言聊天室成员
     * DateTime 2024/9/3 14:59
     * User Admin
     * @param $roomId
     * @param $operator
     * @param $target
     * @param $muteDuration
     * @return true
     */
    public function muteMember($roomId, $operator, $target, $muteDuration)
    {
        $url = "/chatroom/temporaryMute.action";
        $args = [
            "roomid" => $roomId,
            "target" => $target,
            "operator" => $operator,
            'muteDuration' => $muteDuration,
        ];
        logInfo("mute_member", ['uid' => $this->from, 'args' => $args]);
        $result = $this->postDataCurl($url, $args);
        logInfo("mute_member", ['uid' => $this->from, 'result' => $result]);
        if ($result["code"] !== 200) {
            throw new MessageException($result['desc'], CodeData::NEED_TIPS, [], false);
        }

        return true;
    }

    /**
     * Note - 禁言聊天室
     * DateTime 2024/9/3 14:59
     * User Admin
     * @param $roomId
     * @param $operator
     * @param bool $mute
     * @return true
     */
    public function muteRoom($roomId, $operator, $mute)
    {
        $url = "/chatroom/muteRoom.action";
        $args = [
            "roomid" => $roomId,
            "operator" => $operator,
            "mute" => $mute,
        ];
        logInfo("mute_room", ['uid' => $this->from, 'args' => $args]);
        $result = $this->postDataCurl($url, $args);
        logInfo("mute_room", ['uid' => $this->from, 'result' => $result]);
        if ($result["code"] !== 200) {
            throw new MessageException($result['desc'], CodeData::NEED_TIPS, [], false);
        }
        return true;
    }


}